package com.leetcode.周赛.第232场;

/**
 * @author: xiaomi
 * @date: 2021/3/14
 * @description: 5701. 仅执行一次字符串交换能否使两个字符串相等
 * https://leetcode-cn.com/contest/weekly-contest-232/problems/check-if-one-string-swap-can-make-strings-equal/
 */
public class A_5701_仅执行一次字符串交换能否使两个字符串相等 {

    public static void main(String[] args) {

    }

    /**
     * 1.找到不同的位置
     * 2.对该位置进行比较
     *
     * @param s1
     * @param s2
     * @return
     */
    public boolean areAlmostEqual(String s1, String s2) {
        int i1 = -1, i2 = -1;
        char[] chars1 = s1.toCharArray();
        char[] chars2 = s2.toCharArray();
        int len = chars1.length;
        if (len == 1) {
            if (chars1[0] == chars2[0]) {
                return true;
            } else {
                return false;
            }
        }
        //遍历判断
        int count = 0;
        for (int i = 0; i < len; i++) {
            if (chars1[i] != chars2[i]) {
                if (count == 2) {
                    return false;
                }
                count++;
                if (i1 == -1) {
                    i1 = i;
                } else {
                    i2 = i;
                }
            }
        }
        if (count == 0) {
            return true;
        }
        if (count == 1) {
            return false;
        }
        if (chars1[i1]==chars2[i2]&&chars1[i2]==chars2[i1]){
            return true;
        }else {
            return false;
        }

    }
}
